home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: help with strcmp
- Date: Sat, 06 Apr 96 19:32:07 GMT
- Organization: none
- Message-ID: <828819127snz@genesis.demon.co.uk>
- References: <4jpiek$lp6@blaze.cs.jhu.edu> <DpAI6o.2Cq@iquest.net> <4jup7d$8s5@solutions.solon.com> <4jurqhINNao@anvil.ugrad.cs.ubc.ca>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4jurqhINNao@anvil.ugrad.cs.ubc.ca>
- c2a192@ugrad.cs.ubc.ca "Kazimir Kylheku" writes:
-
- >In article <4jup7d$8s5@solutions.solon.com>,
- >Peter Seebach <seebs@solutions.solon.com> wrote:
- >>Once again: feof() informs you only that the last read failed; it doesn't
- >>belong before a read.
- >
- >Whoa, are you implying that the feof() status is _undefined_ before you do the
- >first read or merely that it's an indication of bad style to have the check
- >before a read?
-
- No, feof() is well defined before the first read. However the point is that
- feof() should be tested (if at all) after the corresponding operation, not
- before. That can be no operation if, for example, you don't know whether
- there has been any previous I/O performed on the stream.
-
- >I recently posted some code which does check ferror() and feof()
- >before doing a read, although not in the same sense that Pascal newbies make
- >the mistake. It's the code in which lines from two files are read
- >concurrently (in the lame sense of the word, of course!), and as long as one of
- >them has no ferror() or feof() condition, the loop has to continue. The outer
- >loop does a bottom test, but inside the loop, the entry into two scanning
- >inner-loops is guarded by checking ferror() and feof(), which the first time
- >around happens before anything is read.
-
- The point is that here your feof() and ferror() tests do relate to prior
- reads so even in your example they come after the corresponding read as far
- as the order of execution is concerned. You also perform a test after
- each read in the loop i.e. before the data is 'processed'. However now you
- bring it up I'll comment on the code!
-
- >#include <stdio.h>
- >#include <stdlib.h>
- >#include <errno.h>
-
- I don't see any need for stdlib.h or errno.h.
-
- >void concatprint(FILE *f1, FILE *f2)
- >
- >/*
- > * read lines from f1 and f2 simultaneously, and print them side by side
- > * f1 and f2 must be valid streams open for reading.
- > */
- >
- >{
- > int c;
- >
- > do {
- > if (!feof(f1) && !ferror(f1))
- > while ((c = getc(f1)) != '\n' && c != EOF)
- > putchar(c);
- > if (!feof(f2) && !ferror(f2))
- > while ((c = getc(f2)) != '\n' && c != EOF)
- > putchar(c);
- > putchar('\n');
- > } while ((!feof(f1) && !ferror(f1)) || (!feof(f2) && !ferror(f2)));
- >}
-
- If you hit an error on one of the reads it is questionable as to whether
- the operation as a whole should continue.
-
- The feof() and ferror() tests in the loop check whether the I/O on a file
- has been completed but allows further reading from the other file. There
- are other ways to achieve the same effect e.g. you exit the main loop once
- either of the files has hit end-of-file and then copy the rest of the other
- file as a separate operation. You could also hold the completion indication
- separately e.g.
-
- ...
-
- {
- int c1 = 0, c2 = 0;
-
- do {
- if (c1 != EOF) {
- while ((c1 = getc(f1)) != '\n' && c1 != EOF)
- putchar(c1);
- }
-
- if (c2 != EOF) {
- while ((c2 = getc(f1)) != '\n' && c2 != EOF)
- putchar(c2);
- }
-
- putchar('\n');
- } while (c1 != EOF || c2 != EOF);
- }
-
- avoids the whole issue of feof() by simply not using it!
-
- There is still the issue of whether to continue going on an error. Both of
- these functions should test putchar() for failure.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-